package com.longnuoqi.mydoctor.utils;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Lenovo on 2016/1/5.
 */
public class StringRegex {
    private static final String regex_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";//去除HTML页面中Javascript代码
    private static final String regex_style = "<style[^>]*?>[\\s\\S]*?<\\/script>";//去除HTML页面中CSS样式代码
    private static final String regex_html = "<(\\S*?)[^>]*>.*?|<.*? />";//去除所有的HTML标签
    private static final String regex_space = "\\s*|\t|\r|\n";
    private static String result = null;

    public static String parserString(String str) {
        // String regx  = "<strong.*?>([sS]*?)<>";
        //String regx  = "[\\s\\S]*?<\\/p>";
        String regx = "([\\s\\S]*?)</p>";
        //String regx1="<p.*?>([\s\S]*?)</p>";

        Pattern p = Pattern.compile(regex_script, Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(str);
        result = matcher.replaceAll("");

        p = Pattern.compile(regex_style, Pattern.CASE_INSENSITIVE);
        matcher = p.matcher(result);
        result = matcher.replaceAll("");

        StringBuffer buf = new StringBuffer();
        p = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
        matcher = p.matcher(result);
        while (matcher.find()) {
            Log.e("====================ok", matcher.group());
            buf.append(matcher.group());
        }
        result = buf.toString();
        p = Pattern.compile(regex_space, Pattern.CASE_INSENSITIVE);
        matcher = p.matcher(result);
        result = matcher.replaceAll("");

        p = Pattern.compile(regex_html, Pattern.CASE_INSENSITIVE);
        matcher = p.matcher(result);
        result = matcher.replaceAll("");

        return result;
    }

    public static String getString(String str) {
        String[] array = str.split("<p>");
        StringBuffer buffer = new StringBuffer();
        String aa = null;
        for (int i = 0; i < array.length; i++) {
            buffer.append(array[i].toString() + "\n" + "\t");
            buffer.append("\t");
          /*aa=  parserString(buffer.append(array[i].toString() + "\n" + "\t").toString());*/
            buffer.append(parserString(array[i].toString()) + "\n" + "\t");
        }
        String sa = buffer.toString();
        //去空白
        return sa.replaceAll("\\s*", "");
    }

}

